package kr.inani.android.reference;

import kr.inani.R;
import kr.inani.android.MainActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class DragAndDrop implements OnTouchListener 
{
	private final static int START_DRAGGING = 0;
	private final static int STOP_DRAGGING = 1;	
	private int status;
	private ImageView image;
	private FrameLayout layout;
	private LayoutParams params;
	private Button btn;
	private MainActivity activity;

	protected void onCreate(Bundle savedInstanceState, MainActivity activity)
	{
		this.activity = activity;
		layout = (FrameLayout) activity.findViewById(R.id.tab_book_shelf);
//		btn = (Button) activity.findViewById(R.id.btn);
		btn.setDrawingCacheEnabled(true);
		btn.setOnTouchListener(this);
		
		
		params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);		
	}

	@Override
	public boolean onTouch(View view, MotionEvent me) {
		if (me.getAction() == MotionEvent.ACTION_DOWN) {
			status = START_DRAGGING;
			image = new ImageView(activity);
			image.setImageBitmap(btn.getDrawingCache());
			layout.addView(image, params);
		}
		if (me.getAction() == MotionEvent.ACTION_UP) {
			status = STOP_DRAGGING;
		} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
			if (status == START_DRAGGING) {
				image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
				image.invalidate();
			}
		}
		return false;
	}
	
}
